﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Objects;
using Course.Models;

namespace Course.DAO
{
    public class CategoryDAO
    {
        
        /// <summary>
        /// 修改分类信息
        /// </summary>
        /// <param name="category"></param>
        /// <returns></returns>
        public bool EditOne(Category category)
        {
            try
            {
                var query = DBHelper.Entity.Categories.Single(c => c.CategoryID == category.CategoryID);
                query.CName = category.CName;
                query.CUrl = category.CUrl;
                query.OrderNum = category.OrderNum;
                query.ParentID = category.ParentID;
                query.Statu = category.Statu;
                int count = DBHelper.Entity.SaveChanges();

                if (count != -1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception)
            {
                return false;
                throw;
            }
        }

        /// <summary>
        /// 根据顺序获得分类信息
        /// </summary>
        /// <param name="order"></param>
        /// <returns></returns>
        public Category GetOneByOrder(int order)
        {
            return DBHelper.Entity.Categories.SingleOrDefault(c => c.OrderNum == order);
        }

        /// <summary>
        /// 获取一条分类信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Category GetOne(int id)
        {
            return DBHelper.Entity.Categories.SingleOrDefault(c => c.CategoryID == id);
        }

        /// <summary>
        /// 获取所有的分类信息
        /// </summary>
        /// <returns></returns>
        public IQueryable<Category> GetAll()
        {
            return DBHelper.Entity.Categories.Select(c => c).OrderBy(c => c.LevelCount).OrderBy(c => c.OrderNum);
        }

        /// <summary>
        /// 添加分类
        /// </summary>
        /// <param name="category"></param>
        /// <returns></returns>
        public bool AddOne(Category category)
        {
            try
            {
                DBHelper.Entity.AddToCategories(category);
                int count = DBHelper.Entity.SaveChanges();
                if(count!=-1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception)
            {
                return false;
                throw;
            }  
        }

        public bool DeleteOne(int id)
        {
            try
            {
                var query = DBHelper.Entity.Categories.SingleOrDefault(c => c.CategoryID == id);

                if(query.Articles.Count==0)
                {
                    DBHelper.Entity.DeleteObject(query);
                    int count = DBHelper.Entity.SaveChanges();

                    if(count!=-1)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                else
                {
                    return false;
                }

            }
            catch (Exception)
            {
                return false;
                throw;
            }
        }


        /// <summary>
        /// 获得子分类
        /// </summary>
        /// <param name="pid"></param>
        /// <returns></returns>
        public IQueryable<Category> GetSub(int pid)
        {
            try
            {
                var query =
                    DBHelper.Entity.Categories.Where(c => c.ParentID == pid).OrderBy(c => c.OrderNum);
                return query;
            }
            catch (Exception)
            {
                return null;
                throw;
            }
        }

        /// <summary>
        /// 获得文章所有分类
        /// </summary>
        /// <returns></returns>
        public IQueryable<Category> GetArticleCate()
        {
            try
            {
                var query =
                    DBHelper.Entity.Categories.Where(c => c.Statu == true).OrderBy(c => c.LevelCount).OrderBy(
                        c => c.OrderNum);
                return query;
            }
            catch (Exception)
            {
                return null;
                
                throw;
            }
        }

        /// <summary>
        /// 获得下载的所有分类
        /// </summary>
        /// <returns></returns>
        public IQueryable<Category> GetDownloadCate()
        {
            try
            {
                var query =
                    DBHelper.Entity.Categories.Where(c => c.Statu == false).OrderBy(c => c.LevelCount).OrderBy(
                        c => c.OrderNum);
                return query;
            }
            catch (Exception)
            {

                return null;
                throw;
            }
        }

    }
}